Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AutoRegisterRequestProcessors to include all implementations #989

Merged

Conversation

hisuwh
Copy link
Contributor

@hisuwh hisuwh commented Dec 29, 2023

This is how this previously worked. When this option was restored the addIfAlreadyExists property was mistakenly set to false.

I have updated the tests and changed this property.

@jbogard
Copy link
Owner

jbogard commented Jan 9, 2024

Will this double-register processors?

@hisuwh
Copy link
Contributor Author

hisuwh commented Jan 9, 2024

I don't believe so. I didn't want to make the tests too brittle by asserting based upon the Pre/Post-processors currently defined.
I.e. I could have done

preProcessors.Count.ShouldBe(4);

But then if another PreProcessor is added to cover another test case this test will break.

Without the fix the second assertion here fails - because it only registers the first type:

preProcessors.ShouldContain(p => p != null && p.GetType() == typeof(FirstConcretePreProcessor));
preProcessors.ShouldContain(p => p != null && p.GetType() == typeof(NextConcretePreProcessor)); // fail

I'm happy to change the tests here to make them more explicit - just be aware this will make them potentially more brittle.

Looking at the code:

private static void ConnectImplementationsToTypesClosing(Type openRequestInterface,

The only way I can see a sort of duplicate registration would be if you had something like this:

public class Processor1 : IRequestPreProcessor<Ping>
{
}

public class Processor2 : Processor1, IRequestPreProcessor<Ping>
{
}

But I can't think of a legitimate reason to do that anyway.

@asimmon
Copy link
Contributor

asimmon commented Jan 9, 2024

Hi @jbogard, I agree with @hisuwh's claim that there is a bug in the way the new AutoRegisterRequestProcessors boolean flag works. Here's the simplest repro case I've got. The pre/post processors are simply not executed. I believe this is the most trivial use of processors.

// <PackageReference Include="MediatR" Version="12.2.0" />
// <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />

using MediatR;
using MediatR.Pipeline;
using Microsoft.Extensions.DependencyInjection;

var services = new ServiceCollection().AddMediatR(x =>
{
    x.AutoRegisterRequestProcessors = true;
    x.RegisterServicesFromAssemblyContaining<Program>();
});

await using var serviceProvider = services.BuildServiceProvider();
await serviceProvider.GetRequiredService<IMediator>().Send(new MyRequest());

public class MyRequest : IRequest;

public class MyRequestHandler : IRequestHandler<MyRequest>
{
    public Task Handle(MyRequest request, CancellationToken cancellationToken) => Task.CompletedTask;
}

public class MyRequestProcessor : IRequestPreProcessor<MyRequest>, IRequestPostProcessor<MyRequest, Unit>
{
    public Task Process(MyRequest request, CancellationToken cancellationToken) => Task.CompletedTask;
    public Task Process(MyRequest request, Unit response, CancellationToken cancellationToken) => Task.CompletedTask;
}

It's very unfortunate as we were about to leverage pre/post processors in one of my team's features. Our only option right now is to downgrade MediatR to 12.0.1, which involves not benefiting from the recent improvements and other bug fixes.

@bryanboettcher
Copy link

+1 for "this is impacting me"

@hisuwh
Copy link
Contributor Author

hisuwh commented Jan 15, 2024

@jbogard do you want me to change anything on this or add any more tests? I'm happy to do so.
Otherwise, can we get this in?

@jbogard jbogard merged commit 88c4d73 into jbogard:master Jan 17, 2024
4 checks passed
@jbogard
Copy link
Owner

jbogard commented Jan 17, 2024

That scanning stuff is code I copied from some other DI projects, so you can see why I don't really want to copy more behaviors from DI containers because that code is...complicated

@hisuwh hisuwh deleted the bug/not-all-pre-post-processors-registered branch January 17, 2024 14:55
@hisuwh
Copy link
Contributor Author

hisuwh commented Apr 16, 2024

@jbogard when are you planning new release with this in please 🙏🥺

oskogstad referenced this pull request in digdir/dialogporten Jun 16, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [MediatR](https://togithub.com/jbogard/MediatR) | `12.2.0` -> `12.3.0`
|
[![age](https://developer.mend.io/api/mc/badges/age/nuget/MediatR/12.3.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/nuget/MediatR/12.3.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/nuget/MediatR/12.2.0/12.3.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/nuget/MediatR/12.2.0/12.3.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>jbogard/MediatR (MediatR)</summary>

###
[`v12.3.0`](https://togithub.com/jbogard/MediatR/releases/tag/v12.3.0)

#### What's Changed

- Fix AutoRegisterRequestProcessors to include all implementations by
[@&#8203;hisuwh](https://togithub.com/hisuwh) in
[https://github.com/jbogard/MediatR/pull/989](https://togithub.com/jbogard/MediatR/pull/989)
- [#&#8203;1016](https://togithub.com/jbogard/MediatR/issues/1016) Use
repo readme for base package by
[@&#8203;thompson-tomo](https://togithub.com/thompson-tomo) in
[https://github.com/jbogard/MediatR/pull/1017](https://togithub.com/jbogard/MediatR/pull/1017)
- Add Support for Generic Handlers by
[@&#8203;zachpainter77](https://togithub.com/zachpainter77) in
[https://github.com/jbogard/MediatR/pull/1013](https://togithub.com/jbogard/MediatR/pull/1013)

#### New Contributors

- [@&#8203;hisuwh](https://togithub.com/hisuwh) made their first
contribution in
[https://github.com/jbogard/MediatR/pull/989](https://togithub.com/jbogard/MediatR/pull/989)
- [@&#8203;thompson-tomo](https://togithub.com/thompson-tomo) made their
first contribution in
[https://github.com/jbogard/MediatR/pull/1017](https://togithub.com/jbogard/MediatR/pull/1017)
- [@&#8203;zachpainter77](https://togithub.com/zachpainter77) made their
first contribution in
[https://github.com/jbogard/MediatR/pull/1013](https://togithub.com/jbogard/MediatR/pull/1013)

**Full Changelog**:
jbogard/MediatR@v12.2.0...v12.3.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "before 7am on Sunday,before 7am on
Wednesday" (UTC), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/digdir/dialogporten).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4zOTMuMCIsInVwZGF0ZWRJblZlciI6IjM3LjM5My4wIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Ole Jørgen Skogstad <skogstad@softis.net>
@asimmon
Copy link
Contributor

asimmon commented Jun 25, 2024

I thought this change would fix this very simple usecase but it still doesn't work with 12.3.0:

#989 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants